<!DOCTYPE html>
<!--
  The MIT License (MIT)

  Copyright (c) 2017 Tarek Sherif

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
    <script src="../build/picogl.js"></script>
    <style>
        html {
            overflow: hidden;
        }

        body {
            margin: 0;
        }
    </style>
</head>
<body>
    <canvas id="gl-canvas"></canvas>
    <script type="x-shader/vs" id="vertex-draw">
        #version 300 es
        
        layout(location=0) in vec4 position;
        layout(location=1) in vec2 uv;
        
        out vec2 vUV;
        void main() {
            vUV = uv;
            gl_Position = position;
        }
    </script>
    <script type="x-shader/vf" id="fragment-draw">
        #version 300 es
        precision highp float;

        in vec2 vUV;

        uniform sampler2D tex;

        out vec4 fragColor;
        void main() {
            fragColor = texture(tex, vUV);
        }
    </script>
    <script type="text/javascript">
        var canvas = document.getElementById("gl-canvas");

        if (!canvas.getContext("webgl2")) {
            console.error("WebGL 2 not available");
            document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
        }

        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        
        var app = PicoGL.createApp(canvas);

        // PROGRAM
        var vsSource =  document.getElementById("vertex-draw").text.trim();
        var fsSource =  document.getElementById("fragment-draw").text.trim();
        var program = app.createProgram(vsSource, fsSource);

        // FRAMEBUFFER
        var colorTarget = app.createTexture2D(app.width, app.height);
        var depthTarget = app.createTexture2D(app.width, app.height, {
            format: PicoGL.DEPTH_COMPONENT
        });

        var rttBuffer = app.createFramebuffer().colorTarget(0, colorTarget).depthTarget(depthTarget);

        // GEOMETRY
        var positions = app.createVertexBuffer(PicoGL.FLOAT, 2, new Float32Array([
            -0.5, 0.5,
            -0.5, -0.5,
            0.5, -0.5,
            -0.5, 0.5,
            0.5, -0.5,
            0.5, 0.5
        ]));
        var uv = app.createVertexBuffer(PicoGL.FLOAT, 2, new Float32Array([
            0, 1,
            0, 0,
            1, 0,
            0, 1,
            1, 0,
            1, 1
        ]));

        var quadArray = app.createVertexArray()
        .attributeBuffer(0, positions)
        .attributeBuffer(1, uv);

        var image = new Image();

        image.onload = function() {
            var texture = app.createTexture2D(image, { flipY: true });

            var drawCall = app.createDrawCall(program, quadArray);

            // RENDER TO OFFSCREEN TEXTURE
            app.drawFramebuffer(rttBuffer).clearColor(0.4, 0.4, 0.4, 1.0).clear();
            drawCall.texture("tex", texture).draw();
            
            // RENDER TO SCREEN
            app.defaultDrawFramebuffer().clearColor(0.0, 0.0, 0.0, 1.0).clear();
            drawCall.texture("tex", rttBuffer.colorAttachments[0]).draw();
            
        }

        image.src = "../examples/img/webgl-logo.png";
    </script>
</body>
</html>
